<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Gestalt Sample: Simple Trigonometry - Python</title>

    <script src="../../js/jquery.js" type="text/javascript"></script>
    <script src="../../js/gestalt.js" type="text/javascript"></script>
</head>
<body>

<script language="python">
# imports
from System import * # for Convert
from System.Windows import * # for NameProperty
from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
from System.Windows.Media.Animation import * # exposes Storyboard
#from System.Windows.Media.Imaging import * # for bitmap
from System.Diagnostics import * # enables outputing to a debug window!

# localize
layoutRoot = me.LayoutRoot
Dots = me.Dots

# locals
mouse = Point(0, 0)
origin = Point(110, 110);
radius = 100
follow = None
tween = None
curAngle = 0;


#
# define class that procedurally creates Tile code instead of XAML
#
class Dot(Canvas): # inherits from Canvas
    def __init__(self):
    
        ### CREATE INSTANCE VARIABLES ###
        layoutRoot = Canvas()
        self.Children.Add(layoutRoot)
        self.kill = 100
        
        self.bkg = Ellipse()
        self.bkg.Width = 8
        self.bkg.Height = 8
        self.bkg.Fill = SolidColorBrush(Color.FromArgb(0xFF,0xFF,0x99,0xFF))
        Canvas.SetLeft(self.bkg, -4)
        Canvas.SetTop (self.bkg, -4)
        layoutRoot.Children.Add(self.bkg)
        
        def sb2_Completed(sender, e):
            if self.kill > 0:
                self.bkg.Opacity = self.kill / 100.0
                self.kill = self.kill - 5
                self.sb2.Begin();
            else:
                (self.Parent).Children.Remove(self)
    
        self.sb2 = Storyboard()
        layoutRoot.Resources.Add("sb2", self.sb2)   
        self.sb2.Completed += sb2_Completed
    
    def fade(self):
            self.sb2.Begin()
        
    ### X property ###
    def SetX(self, value):
        self.SetValue(Canvas.LeftProperty, Convert.ToDouble(value))
    def GetX(self):
        return self.GetValue(Canvas.LeftProperty)
    X = property(GetX, SetX)
    
    ### Y property ###  
    def SetY(self, value):
        self.SetValue(Canvas.TopProperty, Convert.ToDouble(value))
    def GetY(self):
        return self.GetValue(Canvas.TopProperty)
    Y = property(GetY, SetY)

#
# Define event handlers before they are referenced below
#
def Page_MouseMove(sender, e):
    global mouse
    global layoutRoot
    mouse = e.GetPosition(layoutRoot)
    
def setAngle():
    global mouse
    global targetAngle
    global origin
    global follow
    global radius
    global curAngle
    global Dots
    _x = mouse.X - origin.X
    _y = -1 *(mouse.Y - origin.Y)
    targetAngle = Math.Atan2(_y, _x) / (Math.PI / 180)
    if targetAngle < 0: 
        targetAngle = targetAngle + 360
    # we set follow directly to the calculated value
    follow.X = origin.X + (Math.Cos(targetAngle * (Math.PI / 180)) * radius)
    follow.Y = origin.Y - (Math.Sin(targetAngle * (Math.PI / 180)) * radius)
    # Prevent the tween jump by massaging the angle values...
    distance = targetAngle - curAngle
    if distance > 180:
        distance = distance - 360
    if distance <= -180:
        distance = distance + 360
    step = distance * .1
    curAngle = curAngle + step
    if curAngle < 0: 
        curAngle = curAngle + 360
    if curAngle > 360:
        curAngle = curAngle - 360
    tween.X = origin.X + (Math.Cos(curAngle * (Math.PI / 180)) * radius)
    tween.Y = origin.Y - (Math.Sin(curAngle * (Math.PI / 180)) * radius)
    d =  Dot()
    d.X = tween.X
    d.Y = tween.Y
    d.fade()
    Dots.Children.Add(d)   
    
def sb_Completed(sender, e):
    global sb
    setAngle()
    sb.Begin() 

#
# attach event handler
#
layoutRoot.MouseMove += Page_MouseMove

#
# run it
#
d = Dot();
d.X = origin.X;
d.Y = origin.Y;
layoutRoot.Children.Add(d);

follow = Dot()
follow.bkg.Fill = SolidColorBrush(Color.FromArgb(0xcc, 0xff, 0xFF, 0xff))
#follow.SetValue(NameProperty, "follow")
follow.Name = "follow"
follow.X = origin.X + radius
follow.Y = origin.Y
layoutRoot.Children.Add(follow)

tween = Dot()
#tween.SetValue(NameProperty, "tween")
tween.Name = "tween"
tween.X = origin.X + radius
tween.Y = origin.Y
Dots.Children.Add(tween)

sb = Storyboard()
sb.Completed += sb_Completed
layoutRoot.Resources.Add("sb", sb)


sb.Begin()
</script>


    <!-- This isn't a part of the sample, but a link back to the Gestalt site -->
    <div id="upsell" style="color:#fff; font-size: 13px; font-family:'Lucida Grande','Lucida Sans Unicode',Helvetica,Arial,Lucida,Verdana,sans-serif; margin-bottom: 30px;">
        <a href="http://www.visitmix.com/labs/gestalt/samples/" style="background-color: #000; color: #fff; display: block; text-decoration: none;" onmouseover="this.style.color='#000'; this.style.background='#ccc'" onmouseout="this.style.color='#fff'; this.style.background='#000'">
		    <span style="display:block; padding: 8px; text-align:center;">&laquo; Check out other samples on the Gestalt web site</span>
	    </a>
    </div>

<!-- The following commented line of code is equivalent to the inline XAML
     and we recommend doing it this way. Only caveat, if you link to a XAML
     file, you'll be able to view the page correctly only through a server 
     (e.g. apache or IIS) because of security sandbox reasons. -->
    <!--<xml src="page.xaml" id="page" class="xaml" width="600" height="300" style="display:none;"></xml>-->
    
    <xml class="xaml" width="220" height="220">
        <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Document" Width="220" Height="220">
            <Canvas x:Name="LayoutRoot" Background="#111111">
                <Ellipse x:Name="ringOut" Width="210" Height="210" Canvas.Left="5" Canvas.Top="5" Stroke="#cccccc" StrokeDashArray="2 3"></Ellipse>
                <Ellipse x:Name="ringIn" Width="190" Height="190" Canvas.Left="15" Canvas.Top="15" Stroke="#777777" StrokeDashArray="2 3"></Ellipse>
                <Canvas x:Name="Dots"></Canvas>
            </Canvas>
        </Grid>
    </xml>
    
</body>
</html>
